package com.carp.shard.plugin.config;

import com.carp.shard.plugin.constant.ReadType;
import com.carp.shard.plugin.constant.WriteType;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author lile
 * @since 2024/6/5
 */
@Data
@ConfigurationProperties(prefix = "shard")
public class MyBatisShardProperties {

    private List<ShardProperty> shardProperties;
    private Map<String, ShardProperty> shardPropertyMap;

    @PostConstruct
    private void init() {
        shardPropertyMap = shardProperties.stream()
                .collect(Collectors.toMap(ShardProperty::getTableName, Function.identity()));
    }

    @Data
    public static class ShardProperty {
        private String tableName;
        private ReadType readType;
        private WriteType writeType;
    }

}
